﻿using ScFramework.Core;
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace ScFramework.Core
{
    public class GameMode:IGameMode
    {

        Transform map;
        static IGameMode _instance;
        public static IGameMode Instance
        {
            get
            {
                if(_instance == null)
                {
                    Debug.LogError("GameMode is not set yet");
                }
                return _instance;
            }
        }


        public static void SetGamemode(IGameMode mode,Main m)
        {
            _instance = mode;
            mode.Init(m);
        }

        public bool CheckIfCanToGame(List<SColliderVEx> coms)
        {
            if (coms.FindAll(s => typeof(SCore).IsAssignableFrom(s.Data.ComponentType)).Count != 1)
            {
                return false;
            }
            return true;
        }

        public Transform GetMapTransform()
        {
            if (map == null) return map = GameObject.Find("Map").transform;
            else return map;

        }

        public bool PosChecker(Vector2 objPos)
        {
            var camePos = cam.CameraPos;
        //    Debug.Log($"camera pos is {camePos}");
            if (Mathf.Abs(objPos.x - camePos.x) > cam.ViewSiz.x) return false;
            if (Mathf.Abs(objPos.y - camePos.y) > cam.ViewSiz.y) return false;
            return true;
        }
        /// <summary>
        /// 根据当前实体数据返回需要创建的实例
        /// </summary>
        /// <param name="entities"></param>
        /// <returns></returns>
        public virtual List<EntityData> SelectInstance(List<EntityData>entities,object args=null)
        {
            return entities;
        }
        Main _main=null;

        CameraManager cam = null;
        public void Init(Main main)
        {
            _main = main;
            cam = _main.Get<CameraManager>();
        }
    }
}

public interface IGameMode
{
    List<EntityData> SelectInstance(List<EntityData> entities,object args=null);

    Transform GetMapTransform();

    bool CheckIfCanToGame(List<SColliderVEx> coms);

    void Init(Main main);


    bool PosChecker(Vector2 objPos);

}
